package org.example.configcenter;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

public class FetchWithRestTemplate {

    public static String fetch(String url, String token) {
        RestTemplate restTemplate = new RestTemplate(); // 手动创建

        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer " + token);

        // GET 请求
        ResponseEntity<String> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                new HttpEntity<>(headers),
                String.class
        );

        // POST 请求
        // ResponseEntity<String> response = restTemplate.exchange(
        //     url,
        //     HttpMethod.POST,
        //     new HttpEntity<>("{\"key\":\"value\"}", headers),
        //     String.class
        // );

        return response.getBody();
    }
}
